﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Utilities;

namespace BusinessLogicLayer
{
    public class DuAnQKManager
    {
        public ActionResultAsList<DuAnQuaKhu> LayDS()
        {
            var ar = new ActionResultAsList<DuAnQuaKhu>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    ar.ObjectList = db.DuAnQuaKhus.ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResultAsList<DuAnQuaKhu> LayDSTheoMaHS(int maHS)
        {
            var ar = new ActionResultAsList<DuAnQuaKhu>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    ar.ObjectList = db.DuAnQuaKhus.Where(d => d.MaHoSo == maHS).ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResultAsList<DuAnQuaKhu> LayDSTheoMaNV(string maNV)
        {
            var ar = new ActionResultAsList<DuAnQuaKhu>();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    ar.ObjectList = db.DuAnQuaKhus.Where(d => d.MaNhanVien == maNV).ToList();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ar.AddException(ex);
            }
            return ar;
        }

        public ActionResult Them(DuAnQuaKhu da)
        {
            var ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    da = db.DuAnQuaKhus.Add(da);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }

        public ActionResult CapNhat(DuAnQuaKhu da)
        {
            var ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var da1 = db.DuAnQuaKhus.Single(d => d.MaDAQK == da.MaDAQK);
                    da1.MaHoSo = da.MaHoSo;
                    da1.MaNhanVien = da.MaNhanVien;
                    da1.TenDuAn = da.TenDuAn;
                    db.SaveChanges();
                    da = da1;
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }

        public ActionResult Xoa(int maDAQK)
        {
            var ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    var da = db.DuAnQuaKhus.Single(d => d.MaDAQK == maDAQK);
                    db.DuAnQuaKhus.Remove(da);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }

        public ActionResult XoaBenHoSo(int maHoSo)
        {
            var ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    foreach (var da in db.DuAnQuaKhus.Where(d => d.MaHoSo == maHoSo))
                    {
                        if (da.MaNhanVien != null)
                            da.MaHoSo = null;
                        else
                            db.DuAnQuaKhus.Remove(da);
                    }
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }

        public ActionResult XoaBenNhanVien(string maNV)
        {
            var ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    foreach (var da in db.DuAnQuaKhus.Where(d => d.MaNhanVien == maNV))
                    {
                        if (da.MaHoSo != null)
                            da.MaNhanVien = null;
                        else
                            db.DuAnQuaKhus.Remove(da);
                    }
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }

        public ActionResult CapNhatDAChoNV(int maHS, string maNV)
        {
            var ac = new ActionResult();
            try
            {
                using (var db = new QLNVDbContext())
                {
                    foreach (var da in db.DuAnQuaKhus.Where(d => d.MaHoSo == maHS))
                    {
                        da.MaNhanVien = maNV;
                    }
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Write(ex);
                ac.AddException(ex);
            }
            return ac;
        }
    }
}
